package red.mlz.app.controller.category;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import red.mlz.app.domain.category.CategorylListVo;
import red.mlz.module.module.category.entity.Category;
import red.mlz.module.module.category.service.CategoryService;
import red.mlz.module.utils.Response;

import java.math.BigInteger;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 分类表 前端控制器
 * </p>
 *
 * @author mlzred
 * @since 2025-04-03
 */
@RestController
@Slf4j
public class CategoryController {


    @Autowired
    private CategoryService categoryService;


    /**
     * 首先展现的是二级标题
     *
     * @return
     */
    @RequestMapping("/category/list")
    public Response categoryList() {
        // 1. 查询一级分类
        List<Category> oneCategoryList = categoryService.listByParentId("0");
        if (CollectionUtils.isEmpty(oneCategoryList)) {
            return new Response(1001, Collections.emptyList());
        }
        String oneIds = oneCategoryList.stream()
                .map(Category::getId)
                .map(String::valueOf)
                .collect(Collectors.joining(","));
        // 2. 查询二级分类
        List<Category> twoCategoryList = categoryService.listByParentId(oneIds);
        Map<BigInteger, List<Category>> twoCategoryMap = twoCategoryList.stream()
                .collect(Collectors.groupingBy(Category::getParentId));

        List<CategorylListVo> results = oneCategoryList.stream()
                .map(one -> {
                    CategorylListVo parentVO = new CategorylListVo()
                            .setCategoryId(one.getId())
                            .setCategoryName(one.getName());
                    List<Category> childrenList = twoCategoryMap.get(one.getId());
                    if (!CollectionUtils.isEmpty(childrenList)) {
                        List<CategorylListVo> childrenListVO = childrenList.stream()
                                .map(children -> {
                                    CategorylListVo childrenVO = new CategorylListVo()
                                            .setCategoryId(children.getId())
                                            .setCategoryName(children.getName());
                                    return childrenVO;
                                }).collect(Collectors.toList());
                        parentVO.setChildrenList(childrenListVO);
                    }
                    return parentVO;
                }).collect(Collectors.toList());

        return new Response(1001,results);

    }

    /**
     * 查询二级标题及其以上的子标题
     *
     * @return
     */
    @RequestMapping("/category/listByPid")
    public Response listByPid(@RequestParam(name = "parentId") Integer parentId) {
        List<Category> categoryList = categoryService.listByParentId(parentId.toString());
        // 查询出我的分类
        List<CategorylListVo> results = categoryList.stream()
                .map(category -> {
                    CategorylListVo categorylList2VO = new CategorylListVo();
                    categorylList2VO.setCategoryId(category.getId());
                    categorylList2VO.setCategoryName(category.getName());
                    return categorylList2VO;
                }).collect(Collectors.toList());
        return new Response(1001,results);
    }
}
